import { createRouter, createWebHistory } from 'vue-router'
import store from '../store/index'
import { ElMessage } from 'element-plus'
const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/login',
    name: '',
    component: () => import('../views/login.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/index.vue'),
    meta: {
      isAuthRequired: true
    },
    children: [
      {
        path: '',
        component: () => import("../views/brand.vue")
      },
      {
        path: '/index/brand',
        component: () => import("../views/brand.vue")
      },
      {
        path: '/four',
        component: () => import("../views/NavigatorFour.vue")
      }
    ]
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.isAuthRequired && store.state.user.isLogin == false) {
    ElMessage.warning('请先登录')
    return next('/login');
  } else {
    next();
  }
})
export default router
